import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-12-17
 * Time: 9:43
 */
public class RedisSet {

    public static void test1(Jedis jedis) {
        System.out.println("sadd/smembers");

        jedis.flushAll();
        jedis.sadd("key1","111","222","333");
        Set<String> result = jedis.smembers("key1");
        System.out.println("result = "+result);

        boolean isExist = jedis.sismember("key1", "222");
        System.out.println(isExist);

        isExist = jedis.sismember("key2", "222");
        System.out.println(isExist);
    }

    public static void test2(Jedis jedis) {
        System.out.println("scard");

        jedis.flushAll();
        jedis.sadd("key1","111","222","333","444","555");
        long result = jedis.scard("key1");
        System.out.println("result = "+result);
    }

    public static void test3(Jedis jedis) {
        System.out.println("spop");

        jedis.flushAll();
        jedis.sadd("key1","111","222","333");
        String result1 = jedis.spop("key1");
        System.out.println("result1 = "+result1);

        Set<String> resultSet = jedis.smembers("key1");
        System.out.println("resultSet = "+resultSet);
    }

    public static void test4(Jedis jedis) {
        System.out.println("sinter/sinterstore");
        jedis.flushAll();

        jedis.sadd("key1","1","2","3","4");
        jedis.sadd("key2","3","4","5","6");

        Set<String> sinter = jedis.sinter("key1", "key2");
        System.out.println("sinter = "+sinter);

        jedis.sinterstore("res","key1","key2");
        Set<String> res = jedis.smembers("res");
        System.out.println("res = "+res);

    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6379");
        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
            test4(jedis);
        }
    }
}
